package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.hckj.jianzhijia.model.Identification;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MyIdentificationActivity extends BaseActivity {


    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.tv_temp)
    TextView tvTemp;
    private ProgressDialog progressDialog;
    Identification identification;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_indentification);
        ButterKnife.inject(this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在加载...");
        progressDialog.setCancelable(false);
        GetIdentificationInfoTask getIdentificationInfoTask = new GetIdentificationInfoTask();
        getIdentificationInfoTask.execute();

    }


    class GetIdentificationInfoTask extends AsyncTask<Void, Void, String> {


        @Override
        protected String doInBackground(Void... params) {
            AVQuery avQuery = new AVQuery(Identification.TABLE_NAME);
            avQuery.whereEqualTo("user", jianzhijiaApplacation.getCompanyUser().getAvUser());
            avQuery.orderByDescending("createdAt");
            try {
                List list = avQuery.find();
                if (list.size() == 0) {
                    return "尚未认证";
                } else {
                    AVObject avObject = (AVObject) list.get(0);
                    identification = new Identification(avObject);
                    if (identification.getState() == Identification.STATE_FAIL) {
                        return "认证失败";
                    } else if (identification.getState() == Identification.STATE_ING) {
                        return "认证审核中";
                    } else {
                        return "认证成功";
                    }
                }
            } catch (AVException e) {
                e.printStackTrace();
                return ExceptionUtils.getExceptionInfo(e);
            }
        }

        @Override
        protected void onPreExecute() {
            progressDialog.show();
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            progressDialog.dismiss();
            String content = null;
            if (s.equals("尚未认证")) {

                content = "您的账号尚未认证，是否前去认证";
                showMaterialDialog(content, s, new DialogAction() {
                    @Override
                    public void positiveAction() {
                        startActivity(new Intent(getApplicationContext(),IdentificationActivity.class));
                        finish();
                    }

                    @Override
                    public void negativeAction() {
                        finish();
                    }
                });

            } else if (s.equals("认证失败")) {

                tvTemp.setText("认证失败\n失败原因：" + identification.getFailReason()) ;
               // tvTemp.setBackgroundResource(R.drawable.identification_fail);


            } else if (s.equals("认证审核中")) {
                content = "您的账号正在审核中，管理员将尽快为您进行审核，请您稍等...";
                showMaterialDialog(content, s, new DialogAction() {
                    @Override
                    public void positiveAction() {
                        finish();
                    }

                    @Override
                    public void negativeAction() {
                        finish();
                    }
                });

            } else if (s.equals("认证成功")) {

                tvTemp.setText("认证成功");
               // tvTemp.setBackgroundResource(R.drawable.identifiction_success);

            } else {
                ToastUtils.showToast(getApplicationContext(), s);
                finish();
            }
            super.onPostExecute(s);
        }
    }


    private void showMaterialDialog(String content, String title, final DialogAction dialogAction) {

        Dialog.Builder builder = null;
        builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                fragment.dismiss();
                dialogAction.positiveAction();
                super.onPositiveActionClicked(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                fragment.dismiss();
                dialogAction.negativeAction();
                super.onNegativeActionClicked(fragment);
            }
        };

        ((SimpleDialog.Builder) builder).message(content)
                    .positiveAction("确认")
                    .negativeAction("取消").title(title);
        DialogFragment fragment = DialogFragment.newInstance(builder);
        fragment.setCancelable(false);
        fragment.show(getSupportFragmentManager(), null);
    }


    private interface DialogAction {
        void positiveAction();

        void negativeAction();
    }


}
